package main.staff;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;

import main.util.PYUtil;
import main.util.SqlConstants;

import org.apache.poi.ss.usermodel.Row;

public class MenuAndAuth {
	public String[] menuItem=new String[12];
	
	//功能权限
	public  String[] authItem=new String[8];
	
	public LinkedHashMap<String, String> cols;
	
	public int col;
	
	public Row row;
	
	
	
	public MenuAndAuth(int col, Row row,LinkedHashMap<String, String> cols) {
		this(col,row);
		setCols(cols);
	}

	public MenuAndAuth(int col,Row row){
		menuItem[0]="MENUID";       menuItem[1]="MODULEID";
		menuItem[2]="PARENTID";     menuItem[3]="IMAGEURL";
		menuItem[4]="MENUNAME";     menuItem[5]="MENUDESCRIPTION";
		menuItem[6]="MENUURL";      menuItem[7]="DISPLAYNO";
		menuItem[8]="OPENMODULE";   menuItem[9]="AUTHID";
		menuItem[10]="CLICKTOTAL";  menuItem[11]="ABBREVIATION";
		
		authItem[0]="AUTHID";       authItem[1]="MODULEID";
		authItem[2]="AUTHCODE";     authItem[3]="SUPERCODE";
		authItem[4]="AUTHNAME";     authItem[5]="MENUFLAG";
		authItem[6]="DESCRIPTION";  authItem[7]="ELEMENTURL";
		
		this.col=col;
		this.row=row;
	}
	
	public LinkedHashMap<String, String> getCols() {
		return cols;
	}

	//设置的时候需要设置  authItem[0]="AUTHID";authItem[1]="MODULEID";authItem[3]="SUPERCODE";
	public void setCols(LinkedHashMap<String, String> cols) {
		this.cols = cols;
		String name=SqlConstants.getColValue(row,col);
		if(col==0){//添加名称
			name=name.substring(0, name.indexOf("-"));
		}
		
		this.setAuth(name);
		this.setMenu(name);
		
		if(cols.get(menuItem[7])==null)
			cols.put(menuItem[7], SqlConstants.DISPLAYNO.toString());
	}



	private void setMenu(String name){
		if(col>2){
			return;
		}
		cols.put(menuItem[0], cols.get(authItem[0]));//MENUID
		cols.put(menuItem[2], cols.get(authItem[3]));//PARENTID
		cols.put(menuItem[4], name);//MENUNAME
		cols.put(menuItem[11], PYUtil.getAllFirstLetter(name));//ABBREVIATION
		
		
		
		String openModule=SqlConstants.getColValue(row, 9);
		if(!"".equals("openModule") || "YN".indexOf(openModule)<0){
			openModule="N";
		}
		cols.put(menuItem[8],openModule);//OPENMODULE
		
		if(col==2){
			cols.put(menuItem[3], SqlConstants.getColValue(row, 8));//IMAGEURL
			cols.put(menuItem[5], SqlConstants.getColValue(row, 5));//MENUDESCRIPTION
			cols.put(menuItem[6], SqlConstants.getColValue(row, 4));//MENUURL
			cols.put(menuItem[10], "0");//CLICKTOTAL
		}
	}
	
	private void setAuth(String name){
		cols.put(authItem[2], cols.get(authItem[0]));//AUTHCODE
		cols.put(authItem[4], name);//AUTHNAME
		
		String menuFlag=SqlConstants.getColValue(row, 6);
		cols.put(authItem[5], this.getMenuFlag(menuFlag));//MENUFLAG
		if(col==2){
			cols.put(authItem[6], SqlConstants.getColValue(row, 5));//DESCRIPTION
		}
		if(col==3){
			cols.put(authItem[7], SqlConstants.getColValue(row, 6));//elementURL
		}
	}
	
	public void setButtonAuth(){
		if(col<3){
			return;
		}
		cols.put(authItem[5], "0");//MENUFLAG
		
		String name=cols.get("AUTHNAME");
		String des=SqlConstants.getColValue(row, 5);
		cols.put(authItem[6], des);//DESCRIPTION
		
		if(des!=null && !"".equals(des)){
			String[] sub=des.split("->");
			name=name+"("+sub[sub.length-1]+")";
			cols.put("AUTHNAME", name);
		}
		
	}
	
	public String getMenuFlag(String menuFlag){
		if(col>2){
			return "0";
		}
		
		if(col==2 && "0".equals(menuFlag)){
			return "0";
		}
		
		return "1";
	}
	
	public String getButtonAuthSql(){
		if(cols.get("AUTHNAME")==null || "".equals(cols.get("AUTHNAME")) || col<3){
			return "";
		}
		return SqlConstants.getSql("t_ucp_authelement", cols,authItem);
	}

	public String getAuthSql(){
		if(cols.get("AUTHNAME")==null || "".equals(cols.get("AUTHNAME"))){
			return "";
		}
		return SqlConstants.getSql("t_ucp_authelement", cols,authItem);
	}
	
	public String getMenuSql(){
		if(col>2){
			return "";///*按钮，不添加*/
		}
		if(cols.get("MENUNAME")==null || "".equals(cols.get("MENUNAME"))){
			return "";
		}
		
		return SqlConstants.getSql("t_ucp_menu", cols,menuItem);
	}
	
	public String getMenuSqlCRMSSO(){
		if(col>2){
			return "";///*按钮，不添加*/
		}
		
		String menuURL=cols.get("MENUURL");
		if(menuURL!=null && !"".equals(menuURL)){
			menuURL="http://sso.ngmtt.crm.gx.cmcc:7001/ngmttsso/gxcrm.action?crmurl="+URLEncoder.encode(menuURL);
			cols.put("MENUURL", menuURL);
		}
		
		
		return SqlConstants.getSql("t_ucp_menu", cols,menuItem);
	}
	
	public static void main(String[] args) {
		String[] menuURL=new String[4];
		menuURL[0]="http://cs.crm.gx.cmcc:10501/aiocs/aiocs/specialcust/specialCustCfg.jsp?funcId=16000601&verifyType=&psptReadSerialId=&psptVerifySerialId=";
		
		menuURL[1]="http://cs.crm.gx.cmcc:10501/aiocs/aiocs/sensicust/SensiCustMgnt.jsp?domain=aiocsui&funcId=160113&verifyType=&psptReadSerialId=&psptVerifySerialId=";
		
		menuURL[2]="http://cs.crm.gx.cmcc:10501/aiocs/aiocs/custinfo/worksheet/sheetTemplete.jsp?ACCEPT_FROM=10&tmpl_type=1";
		
		menuURL[3]="http://cs.crm.gx.cmcc:10501/aiocs/aiocs/custinfo/worksheet/sheetTemplete.jsp?ACCEPT_FROM=10";
		
		
		for(String menu:menuURL){
			menu="http://sso.ngmtt.crm.gx.cmcc:7001/ngmttsso/gxcrm.action?crmurl="+URLEncoder.encode(menu);
			System.out.println(menu);
		}
		
		try {
			String test=URLEncoder.encode("https://192.168.100.4:10928/ngesop/gate.html","UTF-8");
			System.out.println(test);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public LinkedHashMap<String, String> createNext(int i){
		String endid="00"+i;
		endid=endid.substring(endid.length()-3);
		
		LinkedHashMap<String, String> nextMap=new LinkedHashMap<String, String>();
		nextMap.put("MODULEID", cols.get("MODULEID"));
		nextMap.put("AUTHID", cols.get("AUTHID")+endid);
		nextMap.put("SUPERCODE", cols.get("AUTHID"));
		nextMap.put("DISPLAYNO", i+"");
		
		return nextMap;
	}
}
